DESTBIN ?= 
CUDA ?= /usr/local/cuda
NVCC ?= $(CUDA)/bin/nvcc
GET_CUDA_GENCODE ?= ../scripts/get_cuda_gencode.sh

GDRAPI_INC := ../include
GDRAPI_SRC := ../src

CUDA_LIB := -L $(CUDA)/lib64 -L $(CUDA)/lib -L /usr/lib64/nvidia -L /usr/lib/nvidia -L $(CUDA)/lib64/stubs
CUDA_INC += -I $(CUDA)/include

CPPFLAGS := $(CUDA_INC) -I $(GDRAPI_INC) -I $(GDRAPI_SRC) -I $(CUDA)/include
LDFLAGS  := $(CUDA_LIB) -L $(CUDA)/lib64 -L $(GDRAPI_SRC)
COMMONCFLAGS := -O2
CFLAGS   += $(COMMONCFLAGS)
CXXFLAGS += $(COMMONCFLAGS)

# Get NVCC flags for compute capabilities from shell script
NVCCFLAGS ?= $(shell $(GET_CUDA_GENCODE) $(NVCC))

LIBS     := -lcuda -lpthread -ldl -lgdrapi

CPP_SRCS := copybw.cpp sanity.cpp copylat.cpp apiperf.cpp
CU_SRCS  := pplat.cu
EXES := $(patsubst %.cpp,gdrcopy_%,$(CPP_SRCS)) $(patsubst %.cu,gdrcopy_%,$(CU_SRCS))

all: exes

exes: $(EXES)

testsuites/testsuite.o: testsuites/testsuite.cpp testsuites/testsuite.hpp common.hpp
common.o: common.cpp $(GDRAPI_INC)/gdrapi.h common.hpp
copybw.o: copybw.cpp $(GDRAPI_INC)/gdrapi.h common.hpp
sanity.o: sanity.cpp $(GDRAPI_INC)/gdrapi.h $(GDRAPI_SRC)/gdrapi_internal.h common.hpp testsuites/testsuite.hpp
copylat.o: copylat.cpp $(GDRAPI_INC)/gdrapi.h common.hpp
apiperf.o: apiperf.cpp $(GDRAPI_INC)/gdrapi.h common.hpp

gdrcopy_copybw: copybw.o common.o
	$(LINK.cc)  -o $@ $^ $(LIBS) -lrt

gdrcopy_sanity: sanity.o common.o testsuites/testsuite.o
	$(LINK.cc)  -o $@ $^ $(LIBS)

gdrcopy_copylat: copylat.o common.o
	$(LINK.cc)  -o $@ $^ $(LIBS) -lrt

gdrcopy_apiperf: apiperf.o common.o
	$(LINK.cc)  -o $@ $^ $(LIBS) -lrt

gdrcopy_pplat: pplat.o common.o
	$(NVCC)  -o $@ $^ $(LDFLAGS) -lgdrapi -lcuda

%.o: %.cu
	$(NVCC) -o $@ -c $^ $(LIBS) $(CPPFLAGS) $(NVCCFLAGS)

clean:
	rm -f *.o $(EXES) *~ core.* testsuites/*.o

install: exes
	@ echo "installing exes in $(DESTBIN)..." && \
	mkdir -p $(DESTBIN) && \
	install -D -v -m u=rwx,g=rx,o=rx gdrcopy_copybw -t $(DESTBIN) && \
	install -D -v -m u=rwx,g=rx,o=rx gdrcopy_copylat -t $(DESTBIN) && \
	install -D -v -m u=rwx,g=rx,o=rx gdrcopy_apiperf -t $(DESTBIN) && \
	install -D -v -m u=rwx,g=rx,o=rx gdrcopy_sanity -t $(DESTBIN) && \
	install -D -v -m u=rwx,g=rx,o=rx gdrcopy_pplat -t $(DESTBIN)
	cd $(DESTBIN) && \
	ln -sf gdrcopy_copybw copybw && \
	ln -sf gdrcopy_copylat copylat && \
	ln -sf gdrcopy_apiperf apiperf && \
	ln -sf gdrcopy_sanity sanity

.PHONY: clean all exes install
